<?php
/**
 * @Author: rocky
 * Date: 2019/9/28
 * Time: 11:22
 */


namespace app\shop\controller\api\v1;

use app\common\controller\BaseApiController;
use app\common\model\ShopMember;
use app\common\model\ShopMemberExpire;
use app\common\model\ShopMemberMarketing;
use app\shop\model\ShopOrder;
use think\Db;
use think\Exception;

/**
 * 定时刷新任务
 * Class TimeOrder
 * @package app\shop\controller\api\v1
 */
class TimeOrder extends BaseApiController
{
    /**
     * 会员自动过期
     */
    public function getCard()
    {
        $data = [
            'total_num' => 0,
            'use_num' => 0,
            'status' => 2
        ];
        $member_list = ShopMember::whereTime('expire_time', '<=', time())->where('status',1)->append(['residue_num'])->select();
        if ($member_list){
//            $expire_data = [];
//            foreach ($member_list as $key => $value){
//                $expire_data[] = [
//                    'uid' => $value['uid'],
//                    'total_num' => $value['total_num'],
//                    'has_num' => $value['residue_num'],
//                    'use_num' => $value['use_num'],
//                    'card_id' => $value['card_id'],
//                    'expire_time' => $value['expire_time'],
//                    'status' => 1
//                ];
//            }
//            (new ShopMemberExpire())->saveAll($expire_data);
            ShopMember::whereTime('expire_time', '<=', time())->where('status',1)->update($data);
        }
        $this->successCode();
    }

    /**
     * 删除关闭订单
     */
    public function getDel()
    {
        ShopOrder::where('status', -1)->whereTime('create_at', '<=', time() - sysconf('order_cancel_del_time') * 60 * 60)->setField('is_deleted', 1);
        $this->successCode();
    }


    /**
     * 自动取消未支付订单
     */
    public function getCancel()
    {
        $orders = ShopOrder::where('status', 1)->whereTime('create_at', '<=', time() - sysconf('order_cancel_time') * 60 * 60)->select();
        foreach ($orders as $order) {
            Db::startTrans();
            try {
                //关闭订单
                $order->status = -1;
                $order->save();
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->errorCode(100,$e->getMessage());
            }
        }
        $this->successCode();
    }

    /**
     * 统计本天的营销报表
     */
    public function memberMarketing()
    {
        $startTime = date('Y-m-d 00:00:00');
        $endTime = date('Y-m-d 23:59:59');

        $shopMemberMarketing = new ShopMemberMarketing;
        $marketingInfo = $shopMemberMarketing->whereBetween('time',[$startTime, $endTime])->find();
        if (!$marketingInfo) {
            $total = Db::name('shop_order')->where('status', 'neq',-1)->where('is_pay',1)->whereBetween('pay_time',[$startTime,$endTime])->sum('total_price');
            $vxTotal = Db::name('shop_order')->where('status', 'neq',-1)->where('pay_type',1)->where('is_pay',1)->whereBetween('pay_time',[$startTime,$endTime])->sum('total_price');
            $data = [];
            $data['time'] = date('Y-m-d H:i:s');
            $data['consumption_total'] = $total;
            $data['wechat_total'] = $vxTotal;
            $data['real_total'] = $total;
            $data['note'] = '数据';
            $shopMemberMarketing->save($data);
        }
    }
}